<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Wallet;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Wallet\Ctl\CoinGiveCtl;
use Srv\Apps\Proto\DTO\Wallet\CoinGiveInvalidDTO;
use Srv\Apps\Proto\DTO\Wallet\CoinGiveListDTO;
use Srv\Apps\Proto\DTO\Wallet\CoinGiveRechargeDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Wallet\CoinGiveListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\AuthData;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class CoinGiveAct extends ActionAbstract
{
    /**
     * @url /coin/give/list
     * @auth getCoinGiveList
     * @req Srv.Apps.Proto.DTO.Wallet.CoinGiveListDTO
     * @rsp Srv.Apps.Proto.VO.Wallet.CoinGiveListVO
     * 看点赠送列表
     */
    public function getCoinGiveList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof CoinGiveListDTO) || !($VOClass instanceof CoinGiveListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = CoinGiveCtl::tag()->getCoinGiveList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /coin/give/invalid
     * @auth invalidCoinGive
     * @req Srv.Apps.Proto.DTO.Wallet.CoinGiveInvalidDTO
     * 看点赠送设置失效
     */
    public function invalidCoinGive(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof CoinGiveInvalidDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $cngeId         = intval($DTOClass->getCngeId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $CoinGiveCtl    = CoinGiveCtl::tag();
        $LogCtl         = LogCtl::tag();
        $coinGiveSrc    = $CoinGiveCtl->getCoinGiveSrc($cngeId);
        $logData        = $setData;
        if(!$CoinGiveCtl->checkCoinGiveValid($coinGiveSrc)){
            $errMsg     = '看点赠送记录无效';
            $LogCtl->logAdd(LogCtl::COIN_GIVE_INVALID, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_ERROR, Lang::getString(LangKeyEnum::_LANG_ERROR));
        }
        if(!$CoinGiveCtl->invalidCoinGive($cngeId, $coinGiveSrc)){
            $errMsg     = '看点赠送记录设置失效失败';
            $LogCtl->logAdd(LogCtl::COIN_GIVE_INVALID, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $logData        = ['old' => $coinGiveSrc];
        $errMsg         = '看点赠送记录设置失效成功';
        $LogCtl->logAdd(LogCtl::COIN_GIVE_INVALID, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /coin/give/recharge
     * @auth rechargeCoinGive
     * @req Srv.Apps.Proto.DTO.Wallet.CoinGiveRechargeDTO
     * 充值看点赠送
     */
    public function rechargeCoinGive(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass, $RouteConfData);
        if(!($DTOClass instanceof CoinGiveRechargeDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $LogCtl             = LogCtl::tag();
        $CoinGiveCtl        = CoinGiveCtl::tag();
        $userId             = intval($DTOClass->getUserId());
        $num                = $DTOClass->getNum();
        $startExpire        = $DTOClass->getStartExpire();
        $endExpire          = $DTOClass->getEndExpire();
        $admUserId          = AuthData::getLoginUserId();
        $logData            = ['userId' => $userId, 'num' => $num, 'startExpire' => $startExpire, 'endExpire' => $endExpire, 'admUserId' => $admUserId];
        $cngeId             = $CoinGiveCtl->rechargeCoinGive($userId, $num, $startExpire, $endExpire);
        if($cngeId < 1){
            $errMsg         = '充值看点赠送失败';
            $LogCtl->logAdd(LogCtl::COIN_GIVE_RECHARGE, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $logData['cngeId']  = $cngeId;
        $errMsg             = '充值看点赠送成功';
        $LogCtl->logAdd(LogCtl::COIN_GIVE_RECHARGE, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}